/**
 *====================================================
 * 文件名称: FinancePro1ductCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月11日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1.dto;

import java.math.BigDecimal;
import java.util.List;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.bulkstock.service.BulkstockInfoService;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.BigDecimalUtils;
import com.yuexunit.finance.context.FinanceConstant;
import com.yuexunit.finance.project.action.v1.model.FinanceProduct;
import com.yuexunit.finance.project.action.v1.model.WarningSchemeResult;
import com.yuexunit.finance.project.action.v1.model.output.FinanceProductDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceProductExpenseDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceProductLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.WarningSchemeDetail;
import com.yuexunit.finance.project.action.v1.model.output.WarningSchemeLineInfo;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceContractEntity;
import com.yuexunit.finance.project.entity.FinanceProductEntity;
import com.yuexunit.finance.project.entity.FinanceProductExpenseEntity;
import com.yuexunit.finance.project.entity.WarningSchemeEntity;
import com.yuexunit.finance.project.entity.WarningSchemeResultEntity;
import com.yuexunit.finance.project.entity.lexicon.ExpenseTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.LoanMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductScopeEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.WarningFactorEnum;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.finance.project.service.FinanceBulkstockService;
import com.yuexunit.finance.project.service.FinanceContractService;
import com.yuexunit.finance.project.service.FinanceProductExpenseService;
import com.yuexunit.finance.project.service.WarningSchemeService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: FinancePro1ductCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月11日 下午7:41:36
 */
public class FinanceProductCastor {

	private static FinanceProductExpenseService financeProductExpenseService = ComponentFactory.loadClass(FinanceProductExpenseService.class);

	private static void completeFinanceProductLineInfo(FinanceProductLineInfo info, FinanceProductEntity persist) {
		BeanUtils.copyProperties(persist, info);
		info.setQuarterSettlement(persist.getQuantitySettlement());
		info.setProductScope(persist.getProductScope().ordinal());
		info.setStartDate(DateUtil.formatDate(persist.getStartDate()));
		info.setEndDate(DateUtil.formatDate(persist.getEndDate()));
		info.setState(persist.getState().ordinal());
		info.setProductType(persist.getProductType().ordinal());
		info.setLoanMethod(persist.getLoanMethod().ordinal());
		if (persist.getBailRate().compareTo(BigDecimal.ZERO) > 0) {
			info.setBail(Boolean.TRUE);
		}
	}

	/** 创建产品 */
	public static FinanceProductEntity fromFinanceProduct(FinanceProduct params) {
		FinanceProductEntity entity = new FinanceProductEntity();
		entity.setLoanMethod(LoanMethodEnum.valueOf(params.getLoanMethod()));
		BeanUtils.copyProperties(params, entity);
		Integer startDate = DateUtil.parseDateInt(params.getStartDate());
		Integer endDate = DateUtil.parseDateInt(params.getEndDate());
		entity.setQuantitySettlement(params.getQuarterSettlement());
		entity.setProductType(ProductTypeEnum.valueOf(params.getProductType()));
		entity.setQuantitySettlement(params.getQuarterSettlement());
		if (startDate > endDate) {
			entity.setStartDate(endDate);
			entity.setEndDate(startDate);
		} else {
			entity.setStartDate(startDate);
			entity.setEndDate(endDate);
		}
		entity.setProductScope(ProductScopeEnum.valueOf(params.getProductScope()));
		if (params.getBail() && params.getBailRate() == null && params.getBailRate().compareTo(BigDecimal.ZERO) <= 0) {
			throw new BusinessException("保证金利率不允许为空");
		}
		if (!params.getBail()) {
			entity.setBailRate(BigDecimal.ZERO);
		}
		return entity;
	}

	public static FinanceProductExpenseDetail toFinanceProductExpense(FinanceProductExpenseEntity fpee) {
		FinanceProductExpenseDetail info = new FinanceProductExpenseDetail();
		BeanUtils.copyProperties(fpee, info);
		info.setExpenseMethod(fpee.getExpenseMethod().ordinal());
		info.setExpenseType(fpee.getExpenseType().ordinal());
		return info;
	}

	public static FinanceProductDetail toFinanceProductDetail(FinanceProductEntity persist) {
		FinanceProductDetail info = new FinanceProductDetail();
		completeFinanceProductLineInfo(info, persist);
		List<FinanceProductExpenseEntity> aheadInterests = financeProductExpenseService.inquireFinanceProductExpense(persist.getProductId(), ExpenseTypeEnum.AHEAD);
		for (FinanceProductExpenseEntity fpee : aheadInterests) {
			FinanceProductExpenseDetail expense = toFinanceProductExpense(fpee);
			expense.setExpenseName("提前罚息");
			expense.setExpenseAmountContent(BigDecimalUtils.getPrettyNumber(fpee.getAmount()) + fpee.getAmountUnit());
			expense.setExpenseDescription(fpee.getExpenseDescription());
			info.getAheadInterests().add(expense);
		}
		List<FinanceProductExpenseEntity> overdueInterests = financeProductExpenseService.inquireFinanceProductExpense(persist.getProductId(), ExpenseTypeEnum.DELAY);
		for (FinanceProductExpenseEntity fpee : overdueInterests) {
			FinanceProductExpenseDetail expense = toFinanceProductExpense(fpee);
			expense.setExpenseName("延期罚息");
			expense.setExpenseAmountContent(BigDecimalUtils.getPrettyNumber(fpee.getAmount()) + fpee.getAmountUnit());
			expense.setExpenseDescription(fpee.getExpenseDescription());
			info.getOverdueInterests().add(expense);
		}

		List<FinanceProductExpenseEntity> expenseInterests = financeProductExpenseService.inquireFinanceProductExpense(persist.getProductId(), ExpenseTypeEnum.EXPENSE);
		for (FinanceProductExpenseEntity fpee : expenseInterests) {
			FinanceProductExpenseDetail expense = toFinanceProductExpense(fpee);
			expense.setExpenseAmountContent(fpee.getAmount() + fpee.getAmountUnit());
			expense.setExpenseDescription(fpee.getExpenseDescription());
			info.getExpenses().add(expense);
		}

		WarningSchemeService warningSchemeService = ComponentFactory.loadClass(WarningSchemeService.class);
		List<WarningSchemeEntity> schemes = warningSchemeService.inquireProductWarningScheme(persist.getProductId());
		for (WarningSchemeEntity scheme : schemes) {
			info.getSchemes().add(FinanceProductCastor.toWarningSchemeDetail(scheme));
			info.getSchemeIds().add(scheme.getSchemeId());
		}
		return info;
	}

	public static FinanceProductLineInfo toFinanceProductLineInfo(FinanceProductEntity persist) {
		FinanceProductLineInfo info = new FinanceProductLineInfo();
		completeFinanceProductLineInfo(info, persist);
		return info;
	}

	/** 返回预警结果 */
	public static WarningSchemeResult toWarningSchemeResult(WarningSchemeResultEntity entity) {
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceBulkstockService financeBulkstockService = ComponentFactory.loadClass(FinanceBulkstockService.class);
		FinanceContractService financeContractService = ComponentFactory.loadClass(FinanceContractService.class);
		
		WarningSchemeResult info = new WarningSchemeResult();
		BeanUtils.copyProperties(entity, info);
		info.setFactor(entity.getFactor().ordinal());
		if (entity != null && entity.getWarningLevel() != null) {
			info.setWarningLevel(entity.getWarningLevel().ordinal());
		}
		if (entity.getFactor() == WarningFactorEnum.BULKSTOCK) {
			info.setRiseFall(entity.getLatestPrice().subtract(entity.getEvaluatePrice()));
			info.setIncrease(info.getRiseFall().divide(entity.getEvaluatePrice(), 2, BigDecimal.ROUND_HALF_UP).multiply(FinanceConstant.HUNDRED));
		}
		
		BulkstockInfoEntity bie = bulkstockInfoService.inquireBulkstockInfo(entity.getBulkstockInfoId());
		info.setSpecification(bie.getSpecificationName());
		
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		info.setFinanceState(finance.getFinanceState().ordinal());
		
		List<FinanceBulkstockEntity> fbes = financeBulkstockService.inquireFinanceBulkstockList(entity.getFinanceId());
		if (entity.getFactor() == WarningFactorEnum.BULKSTOCK) {
			FinanceContractEntity fce = financeContractService.inquireFinanceIdContract(entity.getFinanceId(), 2);
			if (fce != null) {
				info.setFinanceContractSerialNumber(fce.getFinanceContractSerialNumber());
			}
			info.setProductType(finance.getProductType().ordinal());
			info.setWeight(fbes.get(0).getWeight());
			info.setPledgeRate(fbes.get(0).getFinancePrice().divide(entity.getEvaluatePrice(), 2, BigDecimal.ROUND_HALF_UP));
			info.setFinancePrice(fbes.get(0).getFinancePrice());
			info.setLoanAmount(finance.getTotalLoanAmount());
			info.setLatestValuation(entity.getLatestPrice().multiply(entity.getLatestPrice()));
		} else if (entity.getFactor() == WarningFactorEnum.FINANCE_APPLICATION) {
			FinanceContractEntity fce = financeContractService.inquireFinanceIdContract(entity.getFinanceId(), 2);
			if (fce != null) {
				info.setFinanceContractSerialNumber(fce.getFinanceContractSerialNumber());
			}
			info.setProductType(finance.getProductType().ordinal());
			info.setWeight(fbes.get(0).getWeight());
		}
		
		return info;
	}

	/** 返回预警方案 */
	public static void toWarningScheme(WarningSchemeEntity entity, WarningSchemeLineInfo info) {
		BeanUtils.copyProperties(entity, info);
		info.setFactor(entity.getFactor().ordinal());
		info.setValueType(entity.getValueType().ordinal());
		info.setRelationType(entity.getRelationType().ordinal());
		info.setTreatmentMethod(entity.getTreatmentMethod().ordinal());
		info.setFrequency(entity.getFrequency().ordinal());
	}

	/** 返回预警方案 */
	public static WarningSchemeLineInfo toWarningScheme(WarningSchemeEntity entity) {
		WarningSchemeLineInfo info = new WarningSchemeLineInfo();
		toWarningScheme(entity, info);
		return info;
	}

	/** 返回预警方案 */
	public static WarningSchemeDetail toWarningSchemeDetail(WarningSchemeEntity entity) {
		WarningSchemeDetail detail = new WarningSchemeDetail();
		toWarningScheme(entity, detail);
		return detail;
	}

}
